package com.hbgc.controller;

import com.github.pagehelper.PageInfo;
import com.hbgc.common.PageResult;
import com.hbgc.pojo.TbTask;
import com.hbgc.service.ITaskService;
import com.hbgc.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/task")
public class TaskController {

    @Autowired
    private ITaskService iTaskService;


    /**
     * 分页查询
     * @param page
     * @param limit
     * @param taskname
     * @return
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public PageResult queryByPage(@RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "10") int limit, String taskname)
    {
        PageInfo pageInfo=iTaskService.queryTbTaskByPage(page, limit, taskname);
        return ConvertUtils.convertData(pageInfo);
    }



    /**
     * 保存数据
     * @param tbTask
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public  PageResult save(TbTask tbTask)
    {

        boolean status=iTaskService.saveTbTask(tbTask);
        if(status)
        {
            return new PageResult(0,"保存成功");
        }
        else {
            return new PageResult(1,"保存失败");
        }
    }


    /**
     * 修改数据
     * @param tbTask
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public  PageResult update(TbTask tbTask)
    {

        boolean status=iTaskService.updateTbTaskById(tbTask);
        if(status)
        {
            return new PageResult(0,"修改成功");
        }
        else {
            return new PageResult(1,"修改失败");
        }
    }



    /**
     * 根据主键删除爬虫
     * @param taskid
     * @return
     */
    @RequestMapping("/remove/{taskid}")
    @ResponseBody
    public  PageResult remove(@PathVariable Long taskid)
    {

        boolean status=iTaskService.deleteTbTaskById(taskid);
        if(status)
        {
            return new PageResult(0,"删除成功");
        }
        else {
            return new PageResult(1,"删除失败");
        }
    }

    /**
     * 根据主键查询任务
     * @param taskid
     * @return
     */
    @RequestMapping("/querybyid/{taskid}")
    @ResponseBody
    public  PageResult querybyid(@PathVariable Long taskid)
    {
        PageResult pageResult= new PageResult(0,"查询成功");
        TbTask tbTask =iTaskService.getTbTaskById(taskid);
        if(tbTask!=null)
        {
            List data=new ArrayList<>();
            data.add(tbTask);
            pageResult.setData(data);
        }
        else {
            pageResult.setCode(1);
            pageResult.setMsg("查询失败");
        }
        return pageResult;
    }



    /**
     * 启动一个爬虫
     * @param taskid
     * @return
     */
    @RequestMapping("/start/{taskid}")
    @ResponseBody
    public  PageResult start(@PathVariable Long taskid)
    {
        PageResult pageResult= new PageResult(0,"执行成功");
        iTaskService.startTask(taskid);
        return pageResult;
    }


    /**
     * 采集当天的数据资源
     * @param taskid
     * @return
     */
    @RequestMapping("/collectToday/{taskid}")
    @ResponseBody
    public  PageResult collectToday(@PathVariable Long taskid)
    {
        PageResult pageResult= new PageResult(0,"执行成功");
        iTaskService.collectToday(taskid);
        return pageResult;
    }

    /**
     * 采集当天的数据资源
     * @param taskid
     * @return
     */
    @RequestMapping("/collectWeek/{taskid}")
    @ResponseBody
    public  PageResult collectWeek(@PathVariable Long taskid)
    {
        PageResult pageResult= new PageResult(0,"执行成功");
        iTaskService.collectWeek(taskid);
        return pageResult;
    }


    /**
     * 采集所有数据
     * @param taskid
     * @return
     */
    @RequestMapping("/collectAll/{taskid}")
    @ResponseBody
    public  PageResult collectAll(@PathVariable Long taskid)
    {
        PageResult pageResult= new PageResult(0,"执行成功");
        iTaskService.collectAll(taskid);
        return pageResult;
    }

}
